package com.person.jc.service.base.standard;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.common.SQLSpell;
import com.person.jc.service.Service;
import com.person.jc.utils.Utils;

public class BdStandardMeasureScopeService extends Service {

	public final static BdStandardMeasureScopeService me = new BdStandardMeasureScopeService();
	private static String optMenu = "气体设备量程";
	
	public List<Record> getList(Map<String,Object> map){
		String temp = "";
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer("select t.*, ("+SQLSpell.kindName("device_type", "t.device_type")+") deviceTypeName, ("+SQLSpell.deviceName("t.device_id_")+") deviceIdName from bd_standard_measurescope t where if_valid = 1");
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("deviceId_")))){
			sql.append(" and device_id_ = '"+ temp +"'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("deviceType")))){
			sql.append(" and device_type = '"+ temp +"'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("scopeId")))){
			sql.append(" and scope_id = '"+ temp +"'");
		}
		return Db.find(sql.toString(), Utils.getObjParam(params));
	}
	
	public void save(Map<String,Object> map){
		Record record = new Record();
		record.set("measure_scope", map.get("measureScope"));
		record.set("device_type", map.get("deviceType"));
		record.set("remark", map.get("remark"));
		String type = String.valueOf(map.get("deviceType"));
		if("3".equals(type)){
			record.set("device_id_", map.get("deviceId_"));
		}else if("2".equals(type)){
			record.set("device_id_", 8);
		}else if("1".equals(type)){
			record.set("device_id_", 9);
		}
		Db.save("bd_standard_measurescope", record);
		super.setCRUD(optMenu, "C", "新增"+optMenu+"："+type+"-"+map.get("deviceId_"));
		
	}
	
	public void update(Map<String,Object> map){
		Record record = new Record();
		record.set("measure_scope", map.get("measureScope"));
		record.set("device_type", map.get("deviceType"));
		record.set("remark", map.get("remark"));
		String type = String.valueOf(map.get("deviceType"));
		Object deviceId_ = Utils.isNotEmpty(String.valueOf(map.get("deviceId_")))?map.get("deviceId_"):null;
		record.set("device_id_", deviceId_);
		record.set("scope_id",map.get("scopeId"));
		Db.update("bd_standard_measurescope", "scope_id", record);
		super.setCRUD(optMenu, "U", "修改"+optMenu+"："+type+"-"+deviceId_);
		
	}
	
	public void delete(String scopeId){
		String sql = " update bd_standard_measurescope set if_valid = 0 where scope_id = ? ";
		Db.update(sql, scopeId);
		super.setCRUD(optMenu, "D", "删除"+optMenu+"，编号："+scopeId);
	}

	public Map<String,Object> getTemplateHeardData(Integer id){
		String sql="select * from bd_standard_measurescope sm where sm.if_valid=1 and sm.scope_id=?";
		Record record=Db.findFirst(sql,id);
		Map<String,Object> map=new HashMap<String,Object>();
		Integer device_type=null;
		Integer device_id_=null;
		if(record!=null){
			map.put("scopeId",record.getInt("scope_id"));
			map.put("measureScope",record.getStr("measure_scope"));
			device_type=record.getInt("device_type");
			device_id_=record.getInt("device_id_");
		}
		if(device_type!=null){
			String sqlDT="SELECT kind_name FROM ts_code_kind_data WHERE is_show=1 and kind_code = "+device_type+" AND type_code = 'device_type'";
			Record record1=Db.findFirst(sqlDT);
			map.put("deviceType",device_type.toString());
			map.put("deviceTypeName",record1.getStr("kind_name"));
		}
		if(device_id_!=null){
			String sqlDI="SELECT device_name FROM bd_device_info WHERE if_valid=1 and device_id = "+device_id_;
			Record record2=Db.findFirst(sqlDI);
			map.put("deviceId",device_id_.toString());
			map.put("deviceName",record2.getStr("device_name"));
		}

		return map;


	}
}
